<script setup lang="ts">
import Button from './Button.vue';

const props = defineProps({
  visible: { type: Boolean, default: false },
  closeOnClickOverlay: { type: Boolean, default: false },
  ok: Function,
  cancel: Function
})

const emits = defineEmits<{
  (e: 'update:visible', value): void
}>();

const handleClickOverlay = () => {
  if (props.closeOnClickOverlay) {
    close()
  }
}
const close = () => {
  emits('update:visible', false)
}
const ok = () => {
  if (props.ok && props.ok() !== false) close()
}
const cancel = () => {
  if (props.cancel && props.cancel() !== false) close()
}
</script>

<template>
  <template v-if="visible">
    <teleport to="body">

      <div class="gulu-dialog-overlay" @click="handleClickOverlay"></div>

      <div class="gulu-dialog-wrapper">
        <div class="gulu-dialog">
          <header>
            <slot name="title"></slot>
            <span class="gulu-dialog-close" @click="close"></span>
          </header>
          <main>
            <slot name="content"></slot>
          </main>
          <footer>
            <Button level="main" @click="ok">OK</Button>
            <Button @click="cancel">Cancel</Button>
          </footer>
        </div>
      </div>

    </teleport>
  </template>
</template>

<style lang="scss">
$radius: 4px;
$border-color: #d9d9d9;

.gulu-dialog {
  background: white;
  border-radius: $radius;
  box-shadow: 0 0 3px fade-out(black, 0.5);
  min-width: 15em;
  max-width: 90%;
  &-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: fade_out(black, 0.5);
    z-index: 10;
  }
  &-wrapper {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 11;
  }
  > header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 20px;
    padding: 12px 16px;
    border-bottom: 1px solid $border-color;
  }
  > main {
    padding: 12px 16px;
  }
  > footer {
    border-top: 1px solid $border-color;
    padding: 12px 16px;
    text-align: right;
  }
  &-close {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 32px;
    border-radius: 32px;
    cursor: pointer;
    &::before,
    &::after {
      content: "";
      position: absolute;
      height: 1px;
      width: 50%;
      top: 50%;
      left: 50%;
      background: black;
    }
    &::before {
      transform: translate(-50%, -50%) rotate(-45deg);
    }
    &::after {
      transform: translate(-50%, -50%) rotate(45deg);
    }
    &:hover {
      background-color: #f57272;
      &::before,
      &::after {
        background-color: #fff;
      }
    }
  }
}
</style>
